import axios from "axios";
import {ElMessage} from 'element-plus'
import router from '@/router'
const request = axios.create({
  baseURL: '/api',
  timeout: 600000,
});

request.interceptors.request.use( (config) =>{ 
  const loginInfo = localStorage.getItem('loginfo');
  if(loginInfo){
    const loginUser = JSON.parse(loginInfo);
    config.headers.token = loginUser.token;
  }
  return config;
});
request.interceptors.response.use( (response)=>{ 
  return response.data;
},
(error) =>{
  if(error.response.status == 401){
    ElMessage.error('登录失败，请重新登录！');
    router.push('/login');
  }
  return Promise.reject(error);
});

export default request;